//
//  MTMainController.m
//  07-美团外卖
//
//  Created by Apple on 16/10/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "JKJMineViewController.h"
#import "MTThreeBtnView.h"



#define topHeight 123

@interface JKJMineViewController ()<UIScrollViewDelegate,MTThreeBtnViewDelegate>
//头部VIew
@property (nonatomic, weak) UIView *topView;
//三个按钮的VIew
@property (nonatomic, weak) MTThreeBtnView *threeBtnView;
//scrolView
@property (nonatomic, weak) UIScrollView *scrolV;

@end

@implementation JKJMineViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	self.navigationItem.title = @"登录";
	// Do any additional setup after loading the view.
}

#pragma mark - 5.0 监听自定义控件
- (void)threeViewClick:(MTThreeBtnView *)sender
{
	//1.获取显示的rect
	CGRect rect = CGRectMake(sender.selectedIndex * kScreenWidth, 0, kScreenWidth, _scrolV.bounds.size.height);

	//2.显示 自动滚动到指定的位置
	[_scrolV scrollRectToVisible:rect animated:YES];

}
#pragma mark - 4.0 点击按钮实现切换scrolVIew
- (void)threeBtnView:(MTThreeBtnView *)threV withIndex:(NSInteger)selectIndex
{

	//1.获取显示的rect
	CGRect rect = CGRectMake(selectIndex * kScreenWidth, 0, kScreenWidth, _scrolV.bounds.size.height);

	//2.显示 自动滚动到指定的位置
	[_scrolV scrollRectToVisible:rect animated:YES];


}


#pragma mark - 3.0 手动滚动scrolVIew实现小黄条滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	//1.获取滚得变量
	CGPoint offset = scrollView.contentOffset;

#warning 使用技巧 如何判断是手动 还是自动 scroll
	//tracking  轨迹
	//dragging  拖拽
	//decelerating 暂时的停顿

	//    NSLog(@"%zd   %zd  %zd",scrollView.tracking,scrollView.dragging,scrollView.decelerating);

	if (!(scrollView.tracking || scrollView.dragging || scrollView.decelerating)) {//自动


		return;
	}



	//2.赋值给threV的属性
	_threeBtnView.offsetX = offset.x/2;


}



#pragma mark - 1.0界面搭建
-(void)setupUI
{
	//设置导航条的透明度0
	self.navigationController.navigationBar.alpha = 0;


	//MARK:1.topView
	UIView *topV = [[UIView alloc]init];
	topV.backgroundColor = [UIColor purpleColor];
	[self.view addSubview:topV];


	//MARK:2.threeBtnView
	MTThreeBtnView *threeV = [[MTThreeBtnView alloc]init];
	//设置代理
	//    threeV.delegate = self;

	//监听事件
	[threeV addTarget:self action:@selector(threeViewClick:) forControlEvents:UIControlEventValueChanged];


	[self.view addSubview:threeV];


	//MARK:3.scrolView
	UIScrollView *scrolV = [[UIScrollView alloc]init];
	scrolV.backgroundColor = [UIColor brownColor];

	//分页
	scrolV.pagingEnabled = YES;
	//弹动
	scrolV.bounces = NO;
	//滚动条
	scrolV.showsHorizontalScrollIndicator = NO;
	//设置代理
	scrolV.delegate = self;


	[self.view addSubview:scrolV];


	//关联
	_topView = topV;
	_threeBtnView = threeV;
	_scrolV = scrolV;





	//布局
//	[topV mas_makeConstraints:^(MASConstraintMaker *make) {
//		make.left.right.top.equalTo(self.view);
//		make.height.mas_equalTo(topHeight);
//	}];

	[threeV mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.view).offset(64);
		make.left.right.equalTo(self.view);
		make.height.mas_equalTo(50);
	}];

	UIView *hengView = [[UIView alloc] init];
	[self.view addSubview:hengView];
	hengView.backgroundColor = [UIColor hm_colorWithR:0 G:0 B:0 alpha:0.3];
	[hengView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(threeV.mas_bottom);
		make.left.right.equalTo(self.view);
		make.height.equalTo(@(0.5));
	}];

	[scrolV mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.right.bottom.equalTo(self.view);
		make.top.equalTo(threeV.mas_bottom);
	}];


	//MARK:4.添加拖拽手势,监听手势 修改topV 高度约束
	UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

	[self.view addGestureRecognizer:pan];


	//MARK:5.添加 点菜 评价  商家三个控制器的VIew

	NSArray <NSString *>*vcNameArray = @[@"MTCommentController",@"MTBusinessController"];

	NSMutableArray <UIView *>*viewArray = [NSMutableArray array];

	[vcNameArray enumerateObjectsUsingBlock:^(NSString * _Nonnull vcName, NSUInteger idx, BOOL * _Nonnull stop) {

		//1.根据字符串  class
		Class class = NSClassFromString(vcName);

		//2.根据类 生成对象
		UIViewController *targetVc = [[class alloc]init];


		//3.添加(使用的是分类)
		[self cz_addChildController:targetVc intoView:scrolV];


		//4.将VIew 添加集合
		[viewArray addObject:targetVc.view];

	}];

	//将三个控制器的VIew 设置约束
	[viewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
	[viewArray mas_makeConstraints:^(MASConstraintMaker *make) {

		make.top.bottom.equalTo(scrolV);
		//设置大小
		make.size.equalTo(scrolV);


	}];



}



#pragma mark - 2.0监听拖拽的手势
- (void)pan:(UIPanGestureRecognizer *)sender
{

	//MARK:1.获取偏移量
	CGPoint offset = [sender translationInView:sender.view];

	//MARK:2.归零
	[sender setTranslation:CGPointZero inView:sender.view];

	//MARK:3.计算高度     topV原先的自身的高度  +  手势的偏移y
	CGFloat height = _topView.bounds.size.height  + offset.y;


	//MARK:5.判断 64  123;最高123 , 最低64
	if((height > topHeight)  || (height < kNavBarH)){

		return;
	}

	//MARK:4.更新topV的高度的约束
	[_topView mas_updateConstraints:^(MASConstraintMaker *make) {

		make.height.mas_equalTo(height);

	}];

	// 建议加个动画
	[UIView animateWithDuration:.3 animations:^{
		[self.view layoutIfNeeded];
	}];

	//MARK:6.计算透明度
	//透明度 0--1
	//距离  (123- 64)

	//透明度的比例
	CGFloat scale = 1.0/(topHeight - kNavBarH);

	CGFloat alpha =  1 - (height - kNavBarH) * scale;

	self.navigationController.navigationBar.alpha = alpha;



}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

